package 最大子数组和;

public class Solution {
    public static void main(String[] args) {
        int[] nums = new int[]{-2,1,-3,4,-1,2,1,-5,4};
//        int[] nums = new int[]{-1};
//        int[] nums = new int[]{-2,-1};
        System.out.println(maxSubArray(nums));
    }

    public static int maxSubArray(int[] nums) {
        int sum = nums[0];
        int current = nums[0];
        for (int index = 1; index < nums.length;index++) {
            int num = nums[index];
//            if(num <= 0 &&  current == 0 ){
//                continue;
//            }
            if(current < 0 & num > current){
                current = num;
            }
            else{
                current += num;
            }

            if(current > sum){
                sum = current;
            }
        }
        return sum;
    }
}
